package org.example.controller.user;

import lombok.extern.slf4j.Slf4j;
import org.example.annotation.HasRole;
import org.example.annotation.LogTrace;
import org.example.annotation.UserUpdate;
import org.example.dao.dto.user.UserDto;
import org.example.dao.entity.user.UAddress;
import org.example.dao.entity.user.UUser;
import org.example.exception.ParamLossErrorException;
import org.example.service.user.UserServiceImpl;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;
import org.example.dao.commom.Result;

import javax.annotation.Resource;

import org.example.dao.commom.ResultCode;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * (User)控制层
 *
 * @author makejava
 * @since 2023-05-10 10:51:11
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {

    /**
     * 服务对象
     */
    @Resource
    private UserServiceImpl userService;

    //TODO 功能
    /**
     * 获取用户信息(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("/getUserInfo")
    @LogTrace
    public Result getUserInfo() {
        UserDto user = userService.getUserInfo();
        return new Result(ResultCode.SUCCESS, user);
    }
    //TODO 新增
    //TODO 查询
    //TODO 更新
    /**
     * 修改昵称(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("updateNickName")
    @UserUpdate
    public Result updateNickName(@RequestBody UUser user) {
        String nickName = user.getNickName();

        ParamUtil.StringParam(nickName);

        user = userService.updateNickName(nickName);
        return new Result(ResultCode.SUCCESS, user);
    }

    /**
     * 修改头像(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("updateIcon")
    @UserUpdate
    public Result updateIcon(MultipartFile file) {

        if (file == null || file.isEmpty()) {
            throw new ParamLossErrorException("文件未上传");
        }

        UUser user = userService.updateIcon(file);
        return new Result(ResultCode.SUCCESS, user);
    }

    /**
     * 修改性别(用户)
     */
    @PostMapping("updateSex")
    @HasRole(role = "用户")
    @UserUpdate
    public Result updateSex(@RequestBody UUser user) {
        String gender = user.getGender();

        ParamUtil.StringParam(gender);

        gender = userService.updateSex(gender);
        return new Result(ResultCode.SUCCESS, gender);
    }

    /**
     * 修改签名(用户)
     */
    @PostMapping("updateSign")
    @HasRole(role = "用户")
    @UserUpdate
    public Result updateSign(@RequestBody UUser user) {
        String sign = user.getSign();

        ParamUtil.StringParam(sign);

        sign = userService.updateSign(sign);
        return new Result(ResultCode.SUCCESS, sign);
    }

    /**
     * 设置为默认地址(用户)
     */
    @RequestMapping(value = "updateAddressDefault", method = POST)
    @HasRole(role = "用户")
    @UserUpdate
    public Result updateAddressDefault(@RequestBody UAddress uAddress) {

        Long id = uAddress.getId();

        ParamUtil.LongParam(id);

        userService.updateAddressDefault(id);
        return new Result(ResultCode.SUCCESS, null);
    }

    //TODO 删除







































    //TODO 未实现接口
    /**
     * 删除管理类用户(管理)
     */
    @PostMapping("deleteUserStudent")
    public Result deleteUserManager(@RequestBody UUser user) {

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 批量删除管理类用户(管理)
     */
    @PostMapping("deleteUserManagers")
    public Result deleteUserManagers(@RequestBody List<Long> list) {

        return new Result(ResultCode.SUCCESS, null);
    }
    /**
     * 修改管理类用户密码(管理)
     */
    @PostMapping("updateUserManagerPassword")
    public Result updateUserManagerPassword(@RequestBody UUser user) {

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 查询学生用户(管理)
     */
    @PostMapping("selectUserStudent/{limit}/{start}")
    public Result selectUserStudent(@PathVariable("limit") Long limit,
                                    @PathVariable("start") Long start) {


        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 查询管理类用户(管理)
     */
    @PostMapping("selectUserManager/{limit}/{start}")
    public Result selectUserManager(@PathVariable("limit") Long limit,
                                    @PathVariable("start") Long start) {

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 条件判断查询学生用户(管理)
     */
    @PostMapping("selectUserByCondition/{limit}/{start}")
    public Result selectUserStudentByCondition(@PathVariable("limit") Long limit,
                                               @PathVariable("start") Long start) {

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 条件判断查询管理用户(管理)
     */
    @PostMapping("selectUserManagerByCondition/{limit}/{start}")
    public Result selectUserManagerByCondition(@PathVariable("limit") Long limit,
                                               @PathVariable("start") Long start) {

        return new Result(ResultCode.SUCCESS, null);
    }

    /**
     * 管理页登录(管理)
     */
    @PostMapping("/managerLogin")
    public Result managerLogin(@RequestBody UUser user) {

        return null;
    }
    /**
     * 手动添加管理类用户(管理)
     */
    @PostMapping("/addManager")
    public Result addManager(@RequestBody UUser user) {

        return null;
    }


}

